#!/bin/bash
#############################################
## 使用说明
## centos7.2 (x86_64) 软件安装, 需要开通外网访问权限
## 使用方法一:
## 登录机器一键安装 curl https://gitee.com/smartbetter/main-document/raw/master/public/centos7_dev_install.sh | bash && source /etc/profile
## 使用方法二:
## 将脚本上传至机器, 然后登录机器一键安装 bash ./centos7_dev_install.sh && source /etc/profile
#############################################

function install_base(){
  echo "Start install git......"
  yum -y install git

  echo "Start install sshpass......"
  yum -y install sshpass

  echo "Start install vim......"
  yum -y install vim

  echo "Start install openssl......"
  yum -y install openssl openssl-devel

  echo "Start install unzip zip......"
  yum -y install unzip zip

  echo "Start install iptables......"
  yum -y install iptables-services
}

# 安装epel源
function install_epel(){
  echo "Start install epel......"
  yum -y install epel-release
  yum -y install https://repo.ius.io/ius-release-el7.rpm https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
}

# 安装gradle
function install_gradle(){
  if [ -d "/usr/local/gradle-4.10.3" ];then
    # 目录存在
    echo "gradle already installed"
  else
    # 目录不存在, 下载安装
    echo "Start install gradle......"
    wget -P /usr/local https://repo.huaweicloud.com/gradle/gradle-4.10.3-all.zip
    unzip -d /usr/local /usr/local/gradle-4.10.3-all.zip
    rm -rf /usr/local/gradle-4.10.3-all.zip
  fi
}

# 安装maven
function install_maven(){
  if [ -d "/usr/local/apache-maven-3.6.3" ];then
    # 目录存在
    echo "maven already installed"
  else
    # 目录不存在, 下载安装
    echo "Start install maven......"
    wget -P /usr/local https://repo.huaweicloud.com/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
    tar -zxvf /usr/local/apache-maven-3.6.3-bin.tar.gz -C /usr/local
    rm -rf /usr/local/apache-maven-3.6.3-bin.tar.gz
  fi
}

# 安装jdk
function install_jdk(){
  if [ -d "/usr/local/jdk1.8.0_202" ];then
    # 目录存在
    echo "jdk8u202 already installed"
  else
    # 目录不存在, 下载安装
    echo "Start install jdk8u202......"
    wget -P /usr/local https://repo.huaweicloud.com/java/jdk/8u202-b08/jdk-8u202-linux-x64.tar.gz
    tar -zxvf /usr/local/jdk-8u202-linux-x64.tar.gz -C /usr/local
    rm -rf /usr/local/jdk-8u202-linux-x64.tar.gz
  fi
}

# 添加环境变量
function etc_profile(){
  # 根据是否包含特定字符串来决定添不添加
  grep "centos7_dev_install.sh" /etc/profile > /dev/null
  if [ $? -eq 0 ]; then
    echo "/etc/profile already set"
  else
    echo "Start set /etc/profile......"
    echo '
# centos7_dev_install.sh
export JAVA_HOME=/usr/local/jdk1.8.0_202
export MAVEN_HOME=/usr/local/apache-maven-3.6.3
export GRADLE_HOME=/usr/local/gradle-4.10.3
export PATH=$JAVA_HOME/bin:${MAVEN_HOME}/bin:$GRADLE_HOME/bin:$PATH
' >> /etc/profile

    # shell脚本里执行source不生效, 所以需要在命令上执行一下
    #source /etc/profile
  fi
}

# 开始安装
install_epel
install_base
install_gradle
install_maven
install_jdk
# 添加环境变量
etc_profile
exit 0
